package servlet.home;

import com.google.gson.Gson;
import data.Select;
import data.dataStruct.DataStruct;
import filter.PersonagePort;
import servlet.Control;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet(name = "SelectProfileDetail",value = "/home/selectProfileDetail")
public class SelectProfileDetail extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id=request.getParameter("id");

        Map<String,Object> map= Select.profileDetail(Integer.parseInt(id));

        map.remove("visit");

        //根据是否通过管理员验证来决定是否移除priority
        if(!PersonagePort.validationCookie(request,"ROOT","SID")){
            map.remove("priority");

            //根据用户是否登录决定是否移除敏感信息
            if(!PersonagePort.validationCookie(request,"ID","SID")
                    ||request.getSession().getAttribute(id)==null){
                map.remove("pwd");
            }
        }

        //添加城市以及语言
        List<DataStruct> city=Select.profileCity(Integer.parseInt(id));
        List<DataStruct> language=Select.profileLanguage(Integer.parseInt(id));
        map.put("city",city);
        map.put("language",language);

        String json=new Gson().toJson(map);
        Control.sendJson(response,json);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
